package com.qdy.api;

import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;
import com.qdy.entity.Course;
import com.qdy.service.CourseService;
import com.qdy.util.HttpUtil;
import com.qdy.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created by qudingyi on 2017/4/25.
 */
@RequestMapping("/api/v1/course")
@RestController
public class CourseAPIController {
    @Autowired
    private CourseService courseService;


    @RequestMapping(value = "/release" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object release(Course course, @RequestParam("file") MultipartFile file)throws ServiceException {
        try{
            course.setUser(HttpUtil.getUser());
            course.setImgs(UploadUtil.copyFile(file));
            course.setType(2);
            courseService.save(course);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }
    @RequestMapping(value = "/loadMyReleaseCourse",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadMyReleaseCourse()throws ServiceException{
            try{
                return RestResultGenerator.genSuccessResult(courseService.listCourseByUserId(HttpUtil.getUserId()));
             }catch (Exception e){
                throw  new ServiceException(e.getMessage());
            }
    }
    @RequestMapping(value = "/loadVenueReleaseCourse",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadVenueReleaseCourse(Date date)throws ServiceException{
        try{
            return RestResultGenerator.genSuccessResult(courseService.listCourseByVenueAll(date));
        }catch (Exception e){
            throw  new ServiceException(e.getMessage());
        }
    }

    @RequestMapping(value = "/findUserCourse",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findUserCourse(Date date)throws ServiceException{
        try{
            return RestResultGenerator.genSuccessResult(courseService.listCourseByUserAll(date));
        }catch (Exception e){
            throw  new ServiceException(e.getMessage());
        }
    }
    /**
     * 获取用户发布的课程祥情
     * @param courseId
     * @param enrollTime
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getCourseInof/{courseId}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object getUserCourseInfo(@PathVariable("courseId") Long courseId,Date enrollTime)throws ServiceException{
        try{
            return RestResultGenerator.genSuccessResult(courseService.getUserCourseInfo(courseId,enrollTime));
        }catch (Exception e){
            throw  new ServiceException(e.getMessage());
        }
    }


}
